from django.db import models
from django.contrib.auth.models import User

POSITIONS = (
	(10, 'president'),
	(20, 'secretary'),
	(30, 'webmaster'),
	(40, 'treasurer'),
	(50, 'communications'),
	(60, 'social secretary'),
	(70, 'other'),
)

class UserProfile(models.Model):
	user = models.OneToOneField(User) # required
	student_id = models.CharField(max_length=100)

	permissions = (
			("create_user", "Can create a new user"),
			("edit_user", "Can edit user details"),
			("delete_user", "Can delete a user")
		)


class Society(models.Model):
	name = models.CharField(max_length=200)
	slug = models.SlugField(max_length=200, blank=False, unique=True)
	fb_page_id = models.DecimalField(max_digits=50,decimal_places=0, blank=True, null=True)
	fb_access_token = models.CharField(max_length=200, blank=True, null=True)
	fb_page_access_token = models.CharField(max_length=200, blank=True, null=True)
	email_address = models.CharField(max_length=200)
	members = models.ManyToManyField(User, through='Membership')
	description = models.TextField()
	
	def __unicode__(self):
		return self.name
	
	class Meta:
		verbose_name_plural = 'Societies'
		permissions = (
			("create_society", "Can create a new society"),
			("edit_society", "Can edit society details"),
			("delete_society", "Can delete a society"),
			("add_member", "Can add a member to the society"),
			("remove_member", "Can remove a member from the society"),
			("promote_to_admin", "Can promote a member to an admin"),
			("promote_to_manager", "Can promote a member to a manager"),
			("demote_to_manager", "Can demote an admin to a manager"),
			("demote_to_user", "Can demote an admin or manager to a user")
		)


class Membership(models.Model):
	member = models.ForeignKey(User)
	society = models.ForeignKey(Society)
	date_joined = models.DateField(auto_now_add=True)
	public = models.BooleanField()
	position = models.IntegerField(choices=POSITIONS, null=True, blank=True)
	
	def __unicode__(self):
		return '%s -> %s' % (self.member, self.society)
	
	def team_role(self):
		if self.position:
			return filter(lambda x: (x[0] == self.position), POSITIONS)[0][1]
		else:
			return None
	
	class Meta:
		ordering = ['position', 'member']
		
		
	# lass Membership(models.Model):
	# pass
	# 
	# lass Society(models.Model):
	# pass
	# 
	# lass UserProfile(models.Model):
	# pass
